package com.hupu.jrs.ui.imagepreview

import android.graphics.drawable.Animatable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.drawee.drawable.ScalingUtils
import com.facebook.drawee.generic.GenericDraweeHierarchy
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.request.ImageRequest
import com.facebook.imagepipeline.request.ImageRequestBuilder
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.ui.BaseFragment
import com.hupu.jrs.util.ResourceUtil
import com.hupu.jrs.widget.ImageLoadProgressBar
import com.hupu.jrs.widget.photodraweeview.OnViewTapListener
import com.hupu.jrs.widget.photodraweeview.PhotoDraweeView
import fr.castorflex.android.smoothprogressbar.SmoothProgressBar

/**
 * Created by sll on 2016/3/10.
 */
class ImageFragment : BaseFragment() {

    @BindView(R.id.image)
    internal var image: PhotoDraweeView? = null
    @BindView(R.id.progress)
    internal var progress: SmoothProgressBar? = null
    @BindView(R.id.rlProgress)
    internal var rlProgress: RelativeLayout? = null
    @BindView(R.id.tvInfo)
    internal var tvInfo: TextView? = null

    private var url: String? = null

    private val listener = object : BaseControllerListener<ImageInfo>() {

        override fun onFailure(id: String?, throwable: Throwable?) {
            super.onFailure(id, throwable)
            println("onFailure:" + throwable!!.message)
            progress!!.visibility = View.GONE
            tvInfo!!.visibility = View.VISIBLE
            tvInfo!!.text = "图片加载失败"
        }

        override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
            super.onFinalImageSet(id, imageInfo, animatable)
            println("onFinalImageSet")
            if (imageInfo == null) {
                return
            }
            image!!.update(imageInfo.width, imageInfo.height)
            progress!!.visibility = View.GONE
        }

        override fun onSubmit(id: String?, callerContext: Any?) {
            super.onSubmit(id, callerContext)
            println("onSubmit")
            progress!!.visibility = View.VISIBLE
            tvInfo!!.visibility = View.GONE
        }
    }

    override fun initInjector() {

    }

    override fun initContentView(): Int {
        return R.layout.preview_item_layout
    }

    override fun getBundle(bundle: Bundle) {
        url = bundle.getString("url")
    }

    override fun initUI(view: View) {
        ButterKnife.bind(this, view)
        progress!!.isIndeterminate = true
        image!!.setOnViewTapListener(object : OnViewTapListener() {
            override fun onViewTap(view: View, x: Float, y: Float) {
                getActivity().finish()
            }
        })
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            rlProgress!!.setPadding(0, ResourceUtil.getStatusBarHeight(getActivity()), 0, 0)
        }
    }

    override fun initData() {
        showContent(true)
        val request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setProgressiveRenderingEnabled(true)
                .build()

        val hierarchy = GenericDraweeHierarchyBuilder(getResources()).setProgressBarImage(
                ImageLoadProgressBar(object : ImageLoadProgressBar.OnLevelChangeListener() {
                    override fun onChange(level: Int) {
                        if (level > 100 && progress!!.visibility == View.VISIBLE) {
                            progress!!.visibility = View.GONE
                        }
                    }
                }, ResourceUtil.getThemeColor(getActivity()))).build()
        hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER)

        val controller = Fresco.newDraweeControllerBuilder()
        controller.setControllerListener(listener)
        controller.setImageRequest(request)
        controller.oldController = image!!.getController()
        controller.autoPlayAnimations = true
        image!!.setHierarchy(hierarchy)
        image!!.setController(controller.build())
    }

    companion object {

        fun newInstance(url: String): ImageFragment {
            val mFragment = ImageFragment()
            val bundle = Bundle()
            bundle.putString("url", url)
            mFragment.setArguments(bundle)
            return mFragment
        }
    }
}
